package com.eastseeway.radioproject.gmn;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.LiveData;

import com.eastseeway.radioproject.RadioApplication;
import com.eastseeway.radioproject.base.BaseRepository;
import com.eastseeway.radioproject.base.Const;
import com.eastseeway.radioproject.database.RadioProjectDatabase;
import com.eastseeway.radioproject.database.UserInfoDao;
import com.eastseeway.radioproject.database.VBusDao;
import com.eastseeway.radioproject.entities.BaseResponse;
import com.eastseeway.radioproject.entities.ElectrodeInfoBean;
import com.eastseeway.radioproject.entities.OperationParams;
import com.eastseeway.radioproject.retrofit.RetrofitClient;
import com.eastseeway.radioproject.utils.SPHelper;
import com.eastseeway.radioproject.utils.SaveHelper;
import com.eastseeway.radioproject.utils.XLogUtils;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import retrofit2.Call;

public class NPRepository extends BaseRepository {
    private final UserInfoDao mUserInfoDao;
    private final VBusDao mVBusDao;

    public NPRepository() {
        super();
        mUserInfoDao = RadioProjectDatabase.getInstance(RadioApplication.getInstance()).getUserInfoDao();
        mVBusDao = RadioProjectDatabase.getInstance(RadioApplication.getAppContext()).getVBusDao();
    }

    public void saveData(String content){
//        executorService.submit(() -> SaveHelper.write2File(content));
        executorService.submit(() -> XLogUtils.logUserAction(content));
    }

    //    保存用户的快捷操作信息
    public void updateOperationParams(OperationParams operationParams){
        executorService.submit(() -> {
            mUserInfoDao.updateOperationParams(operationParams);
            try {
                Runtime.getRuntime().exec("sync");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public LiveData<List<OperationParams>> queryOperationParams(String username){
        return mUserInfoDao.queryOperationParams(username, false);
    }

    Call<BaseResponse<ElectrodeInfoBean>> checkElectrode(String deviceid){
        return RetrofitClient.getInstance().getServiceApi().checkElectrode(deviceid);
    }

    public String getVBusValue(String id){
        try {
            Future<String> vBus = executorService.submit(() -> mVBusDao.queryVoltageById(id));
            return vBus.get();
        } catch (ExecutionException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
